package com.geodroid;


import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.geodroid.action.Action;
import com.geodroid.action.ActionCreateMiddle;
import com.geodroid.action.ActionCreatePoint;
import com.geodroid.action.ActionCreateSegment;
import com.geodroid.action.ActionIntersect;
import com.geodroid.action.ActionMove;
import com.geodroid.action.ActionScroll;
import com.geodroid.common.graphics.Graphics2D;
import com.geodroid.euclidian.Drawable;
import com.geodroid.euclidian.DrawableList;
import com.geodroid.euclidian.EuclidianView;

public class GeoView extends SurfaceView implements Runnable,
		SurfaceHolder.Callback {

	private static final String TAG = "GeoView";
	
	EuclidianView view = new EuclidianView();
	
	int mode ;
	Thread thread = null ; 
	SurfaceHolder surfaceHolder;
	float density ;
	
	Action actionToPerform ;
	/*
	 * La liste des élèments à dessiner
	 */
	ArrayList<Drawable> drawList = new ArrayList<Drawable>();

	private DrawableList allDrawableList = new DrawableList();

	volatile boolean running = false;

	
	Graphics2D g2 = new Graphics2D(new Canvas(),new Paint(Paint.ANTI_ALIAS_FLAG)) ; 
	Random random;

	volatile boolean touched = false;
	volatile float touched_x, touched_y;

	public GeoView(Context context) {
		super(context);
		surfaceHolder = getHolder();
		paint() ; 
	}

	public GeoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		surfaceHolder = getHolder();
		paint() ; 
	}

	public GeoView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		surfaceHolder = getHolder();
		paint() ; 
	}

	public void onResumeMySurfaceView() {
		running = true;
		thread = new Thread(this);
		thread.start();
	}

	public void onPauseMySurfaceView() {
		boolean retry = true;
		running = false;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	
	public void run() {
		// TODO Auto-generated method stub
		while (running) {
			
		}
	}
	
	public void paint(){
		Log.d(TAG,"<paint>" );
		if (surfaceHolder.getSurface().isValid()) {
			Canvas canvas = surfaceHolder.lockCanvas() ;
			view.paint(canvas) ; 
			surfaceHolder.unlockCanvasAndPost(canvas);
		}
		Log.d(TAG,"</paint>" );
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d(TAG,"event = "+ event.toString()) ; 
		
		if (actionToPerform != null){
			Log.d(TAG,"Action handled\n ") ;
			actionToPerform.dealWith(event, view) ;
			paint() ;
			return true; // processed
		}
		
		return false; 
		
	}

	
	public void newPoint(){
		actionToPerform = new ActionCreatePoint() ;
	}
	
	public void movePoint(){
		actionToPerform = new ActionMove(1) ;
	}
	
	public void middle() {
		actionToPerform = new ActionCreateMiddle(2) ;
		
	}
	
	public void segment() {
		actionToPerform = new ActionCreateSegment(2) ;
	}
	
	public void scrollView() {
		actionToPerform = new ActionScroll() ;
	}
	
	public void intersect() {
		actionToPerform = new ActionIntersect() ;
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	public void scroll() {
		// TODO Auto-generated method stub
		
	}

	public void scalePlus() {
		// TODO Auto-generated method stub
		view.scale(1.1f) ;
		this.paint() ;
	}

	public void scaleMoins() {
		// TODO Auto-generated method stub
		view.scale(1/1.1f) ;
		this.paint() ;
	}



	



}
